/*
 */

package com.jams.goodox.adapter.viewHolder;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.entity.AnnexBean;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.ViewUtils;
import com.jams.library.fresco.FrescoUtil;

import java.util.List;

/**
 * Created by jams on 2016/6/28.
 * ***
 * Tel: ***
 */

public class PictureHolder {
    private RelativeLayout allView;
    private SimpleDraweeView picture;
    private TextView tvCounts;
    private View view;
    private int width;
    private Context context;
    private int position;
    private List<AnnexBean> imageList;

    public PictureHolder(View view, int width, Context context) {
        this.view = view;
        this.width = width;
        this.context = context;
        allView = (RelativeLayout) view.findViewById(R.id.rl_only_image);
        picture = (SimpleDraweeView) view.findViewById(R.id.sdv_only_image_item);
        tvCounts = (TextView) view.findViewById(R.id.tv_image_counts);
    }

    public void setData(List<AnnexBean> imageLists, int positions, int gap, int maxCounts) {
        this.position = positions;
        this.imageList = imageLists;
        tvCounts.setText(String.valueOf(imageList.size()));
        if (imageList.size() > maxCounts) {
            imageList = imageList.subList(0, maxCounts);
            if (position == maxCounts - 1) {
                tvCounts.setVisibility(View.VISIBLE);
            } else {
                tvCounts.setVisibility(View.GONE);
            }
        }
        int newWidth = (width - DensityUtil.dp2px(context, gap)) / 3;
        if (imageList.size() == 1) {//只有一张图的情况
            int bitWidth = imageLists.get(positions).getWidth();
            int bitHeight = imageLists.get(positions).getHeight();
            if ((bitWidth > bitHeight ? bitWidth : bitHeight) < newWidth * 2) {
                ViewUtils.setAbsListViewLayoutWH(allView, bitWidth, bitHeight);
                picture.setImageURI(imageLists.get(positions).getUrlPath());
            } else {
                if (bitWidth >= bitHeight) {
                    ViewUtils.setAbsListViewLayoutWH(allView, newWidth * 2, (bitHeight * newWidth * 2) / bitWidth);
                    picture.setImageURI(Uri.parse(imageLists.get(positions).getUrlPath()));
                } else {
                    ViewUtils.setAbsListViewLayoutWH(allView,  (bitWidth * newWidth * 2) / bitHeight, newWidth * 2);
                    picture.setImageURI(Uri.parse(imageLists.get(positions).getUrlPath()));
//                    FrescoUtil.loadNetUrl(picture, ViewUtils.getSmallUrl(imageList.get(position).getUrlPath()));//使用小图
                }
            }
            return;
        }

        if (imageList.size() == 4) {
            //只有四张图的情况
            switch (position) {
                case 2:
                    position = -1;
                    break;
                case 3:
                    position--;
                    break;
                case 4:
                    position--;
                    break;
            }
        }
        ViewUtils.setAbsListViewLayoutWH(allView, newWidth, newWidth);
        if (position != -1 && position < imageList.size()) {
//            FrescoUtil.loadNetUrl(picture, ViewUtils.getSmallUrl(imageList.get(position).getUrlPath()));//使用小图
            FrescoUtil.loadNetUrl(picture, imageList.get(position).getUrlPath());//使用原图
        } else {
            picture.setVisibility(View.GONE);
        }
    }

}
